package com.example.controller.administractor;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.dto.CommodityDTO;
import com.example.domain.Commodity;
import com.example.domain.User;
import com.example.service.CommodityService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class AdministratorCommodityController {

	private final CommodityService commodityService;

	private final UserService userService;

	@Autowired
	public AdministratorCommodityController(CommodityService commodityService, UserService userService) {
		this.commodityService = commodityService;
		this.userService = userService;
	}

	/**
	 * 管理员查看所有商品
	 *
	 * @param currentPage 页号
	 * @param pageSize    每页数量
	 * @return 返回商品信息
	 */
	@PostMapping(value = "/administrator/commodity/all", produces = "application/json;charset=utf-8")
	public String getAllCommodity(Integer currentPage, Integer pageSize) {
		CommodityDTO commodityDTO = new CommodityDTO();
		commodityDTO.setCurrentPage(currentPage).setPageSize(pageSize);
		return this.reCommodityStateInfo(commodityDTO);
	}

	/**
	 * 修改商品审核状态
	 *
	 * @param id         商品id
	 * @param auditState 商品状态
	 * @return 返回修改信息
	 */
	@PostMapping(value = "/administrator/commodity/update", produces = "application/json;charset=utf-8")
	public String updateCommodityState(Integer id, Integer auditState) {
		Map<String, Object> message = new HashMap<>(16);
		Commodity commodity = commodityService.getById(id);
		commodity.setAuditState(auditState);
		boolean b = commodityService.updateById(commodity);
		message.put("information", b ? 1 : 0);
		return JSON.toJSONString(message);
	}

	/**
	 * 根据商品名称查询商品
	 *
	 * @param text        商品名称
	 * @param currentPage 页号
	 * @param pageSize    每页数量
	 * @return 返回查询的商品
	 */
	@PostMapping(value = "/administrator/commodity/text", produces = "application/json;charset=utf-8")
	public String getCommodityByText(String text, Integer currentPage, Integer pageSize) {
		CommodityDTO commodityDTO = new CommodityDTO();
		commodityDTO.setText(text).setCurrentPage(currentPage).setPageSize(pageSize);
		return this.reCommodityStateInfo(commodityDTO);
	}

	/**
	 * 根据条件查询商品并处理返回信息（商品审核）
	 *
	 * @param commodityDTO 查询条件
	 * @return 返回商品信息
	 */
	private String reCommodityStateInfo(CommodityDTO commodityDTO) {
		Map<String, Object> message = new HashMap<>(16);
		Page<Commodity> page = commodityService.getCommodityByMIdAndTextForM(commodityDTO);
		List<Map<String, Object>> result = new ArrayList<>();
		for (Commodity commodity : page.getRecords()) {
			User merchant = userService.getById(commodity.getUserId());
			Map<String, Object> map = BeanUtil.beanToMap(commodity);
			map.put("picture", Base64.getEncoder().encodeToString(commodity.getPicture()));
			map.put("userName", merchant.getUserName());
			map.put("telephone", merchant.getTelephone());
			map.put("enableState", merchant.getEnableState());
			result.add(map);
		}
		message.put("information", result);
		message.put("total", result.size());
		return JSON.toJSONString(message);
	}

	/**
	 * 查询所有通过审核的商品
	 *
	 * @param currentPage 页号
	 * @param pageSize    每页数量
	 * @return 返回查询商品的信息
	 */
	@PostMapping(value = "/administrator/commodity/emit", produces = "application/json;charset=utf-8")
	public String getCommodityPass(Integer currentPage, Integer pageSize) {
		CommodityDTO commodityDTO = new CommodityDTO();
		commodityDTO.setAuditState(1).setCurrentPage(currentPage).setPageSize(pageSize);
		return this.reCommodityStateInfo(commodityDTO);
	}

	/**
	 * 修改商品的上架状态和轮播图状态
	 *
	 * @param id           商品id
	 * @param productState 商品状态
	 * @param isCarousel   商品是否为轮播图
	 * @return 返回修改结果
	 */
	@PostMapping(value = "/administrator/commodity/update/audit_state", produces = "application/json;charset=utf-8")
	public String updateCommodityInfo(Integer id, Integer productState, Integer isCarousel) {
		Map<String, Object> message = new HashMap<>(16);
		Commodity commodity = commodityService.getById(id);
		commodity.setProductState(productState);
		commodity.setIsCarousel(isCarousel);
		boolean b = commodityService.updateById(commodity);
		message.put("information", b ? 1 : 0);
		return JSON.toJSONString(message);
	}

	/**
	 * 根据商品名称查询商品
	 *
	 * @param text        商品名称
	 * @param currentPage 页号
	 * @param pageSize    每页数量
	 * @return 返回查询的商品
	 */
	@PostMapping(value = "/administrator/commodity/text/audit_state", produces = "application/json;charset=utf-8")
	public String getCommodityByTextPass(String text, Integer currentPage, Integer pageSize) {
		CommodityDTO commodityDTO = new CommodityDTO();
		commodityDTO.setAuditState(1).setText(text).setCurrentPage(currentPage).setPageSize(pageSize);
		return this.reCommodityStateInfo(commodityDTO);
	}


}
